<style>
    .typeDiv {
        height: 130px;
        width: 100%;
        font-size: 25px;
        border: #c3d9ff solid 2px;
        border-radius: 20px;
    }

    .typeBtn {
        height: 90px;
        width: 100%;
        border: #c3d9ff solid 2px;
        border-top-left-radius: 20px;
        border-top-right-radius: 20px;
        border-bottom: 0;
        font-size: 25px;
    }

    .editBtn {
        height: 40px;
        width: 50%;
        border: #c3d9ff solid 2px;
        border-bottom-left-radius: 20px;
        border-right: 0;
    }

    .deleteBtn{
        height: 40px;
        width: 50%;
        border: #c3d9ff solid 2px;
        border-bottom-right-radius: 20px;
    }

</style>
<div class="layui-container layui-col-lg-offset1" style="margin-top: 80px;">
    <a ew-event="refresh" title="刷新" id="refresh" hidden="hidden"></a>
    <div class="layui-row">
        <div class="layui-col-md2 layui-col-md-offset1" id="add">
            <button type="button" class="layui-btn" id="addBtn"
                    style="height: 130px;width: 100%;border: #c3d9ff solid 2px;border-radius:20px;">
                <i class="layui-icon layui-icon-add-circle-fine" style="font-size: 40px;color: #ceeaff;"></i>
            </button>
        </div>
    </div>
</div>

<script type="text/html" id="typeForm">
    <form lay-filter="typeForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">网站类型</label>
                <div class="layui-input-inline">
                    <input name="name" placeholder="网站类型" type="text" class="layui-input" lay-verify="required"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">网页地址</label>
            <div class="layui-input-inline">
                <input name="url" placeholder="请输入网页地址" type="text" class="layui-input" lay-verify="required"/>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-filter="typeFormSubmit" lay-submit>保存</button>
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            </div>
        </div>
    </form>
</script>
<script type="text/html" id="msSource">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="inputs">
                                <input id="url" class="layui-input" type="text" placeholder="发布地址"/>
                            </div>
                            <div class="inputs">
                                <select id="cycle">
                                    <option id="cycleOpt" value="">请选择任务执行周期</option>
                                </select>
                            </div>
                            <!--<div class="inputs">-->
                            <!--<input id="publishTime" class="layui-input" type="text" placeholder="发布时间"/>-->
                            <!--</div>-->
                            <div class="layui-input-inline" style="padding: 5px">
                                <button id="sourceBtnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索
                                </button>
                                <button id="clearBtn" class="layui-btn icon-btn"><i
                                        class="layui-icon layui-icon-refresh-3"></i>重置
                                </button>
                            </div>
                        </div>
                        <div style="float: right;margin: 5px">
                            <button id="batchAdd" class="layui-btn" style="background-color:#6293ff;padding: 0 14px;"><i
                                    class="layui-icon layui-icon-upload-circle" lay-filter="batchAdd" ></i>批量导入
                            </button>
                            <button id="infoSync" class="layui-btn" style="padding: 0 14px;"><i
                                    class="layui-icon layui-icon-download-circle" lay-filter="execAll"></i>更新信息
                            </button>
                            <button id="execAll" class="layui-btn layui-btn-warm" style="padding: 0 14px;"><i
                                    class="layui-icon layui-icon-ok-circle" lay-filter="execAll"></i>全部执行
                            </button>
                            <button id="startAll" class="layui-btn layui-btn-normal" style="padding: 0 14px;"><i
                                    class="layui-icon layui-icon-play" lay-filter="startAll"></i>全部开始
                            </button>
                            <button id="stopAll" class="layui-btn layui-btn-danger" style="padding: 0 14px;"><i
                                    class="layui-icon layui-icon-pause" lay-filter="stopAll"></i>全部暂停
                            </button>
                        </div>
                    </div>
                    <table class="layui-table" id="sourceTable" lay-filter="sourceTable"></table>
                </div>
            </div>
        </div>
    </div>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="sourceTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="exec">执行</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="start">开始</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="stop">停止</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">详情</a>
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="toolBarAdd"><i class="layui-icon layui-icon-add-1"></i>添加
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="batchDel"><i
                class="layui-icon layui-icon-delete"></i>删除
        </button>
    </div>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="sourceForm">
    <form lay-filter="sourceForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">发布地址</label>
                <div class="layui-input-inline">
                    <input name="url" placeholder="发布地址" type="text" class="layui-input unmodify"
                           lay-verify="required"/>
                </div>
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input name="title" placeholder="请输入标题" type="text" class="layui-input"/>
                </div>

                <label class="layui-form-label">摘要</label>
                <div class="layui-input-inline">
                    <input name="summary" placeholder="请输入摘要" type="text" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">关键词打分</label>
                <div class="layui-input-inline">
                    <input name="score" placeholder="请输入关键词打分" type="text" class="layui-input" lay-verify="percent"/>
                </div>
                <label class="layui-form-label">关键词ID</label>
                <div class="layui-input-inline">
                    <input name="keywordId" placeholder="关键词ID" type="text" class="layui-input unmodify"/>
                </div>
                <label class="layui-form-label">执行周期</label>
                <div class="layui-input-inline">
                    <!--<input name="jobCycleId" placeholder="任务执行周期" type="text" class="layui-input" lay-verify="number"/>-->
                    <select id="jobCycleId" name="jobCycleId" lay-verify="required">
                        <option value="">请选择任务执行周期</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">执行状态</label>
                <div class="layui-input-inline">
                    <!--<input name="jobStatus" placeholder="任务执行状态" type="text" class="layui-input unmodify"-->
                    <!--lay-verify="status"/>-->
                    <select id="jobStatus" name="jobStatus" lay-verify="required" class="unmodify">
                        <option value="">请选择任务执行状态</option>
                        <option value="0">未执行</option>
                        <option value="1">正在执行</option>
                        <option value="2">已停止</option>
                    </select>
                </div>
                <label class="layui-form-label">有效性</label>
                <div class="layui-input-inline">
                    <!--<input name="effect" placeholder="有效性" type="text" class="layui-input unmodify" lay-verify="status"/>-->
                    <select name="effect" lay-verify="required" class="unmodify">
                        <option value="0">无效</option>
                        <option value="1" selected>有效</option>
                    </select>
                </div>
                <label class="layui-form-label">是否有更新</label>
                <div class="layui-input-inline">
                    <!--<input name="hasUpdate" placeholder="网站是否有更新" type="text" class="layui-input unmodify" lay-verify="status"/>-->
                    <select name="hasUpdate" lay-verify="required" class="unmodify">
                        <option value="0" selected>无更新</option>
                        <option value="1">有更新</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">采集深度</label>
                <div class="layui-input-inline">
                    <input name="depth" placeholder="请输入采集深度" type="text" class="layui-input" lay-verify="number"/>
                </div>
                <label class="layui-form-label">下发时间</label>
                <div class="layui-input-inline">
                    <input id="assignTime" name="assignTime" placeholder="任务下发时间" type="text"
                           class="layui-input unmodify" lay-verify="datetime"/>
                </div>
                <label class="layui-form-label">保存会话</label>
                <div class="layui-input-inline">
                    <!--<input name="saveSession" placeholder="请输入是否保存会话" type="text" class="layui-input"-->
                    <!--lay-verify="status"/>-->
                    <select name="saveSession" lay-verify="status">
                        <option value="0" selected>不保存</option>
                        <option value="1">保存</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">网站类型ID</label>
                <div class="layui-input-inline">
                    <input name="typeId" placeholder="网站类型ID" type="text" class="layui-input unmodify"
                           lay-verify="status"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="sourceFormSubmit" lay-submit>保存</button>
            </div>
        </div>
    </form>
</script>
<!--批量导入弹窗-->
<script type="text/html" id="batchAddForm">
    <form lay-filter="batchAddForm" class="layui-form model-form">
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">链接地址</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入链接地址,每行一条" class="layui-textarea" name="urls" lay-verify="required"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">执行周期</label>
            <div class="layui-input-block">
                <select id="batchJobCycleId" name="jobCycleId" lay-verify="required">
                    <option value="">请选择任务执行周期</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="batchAddFormSubmit" lay-submit>保存</button>
            </div>
        </div>
    </form>
</script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'formSelects', 'admin', 'config', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var formSelects = layui.formSelects;
        var admin = layui.admin;
        var config = layui.config;
        var laydate = layui.laydate;
        var mRoles = [];  // 全部角色
        var add = $("#add");

        admin.req("type", {}, function (callData) {
            if (callData.code === 200) {
                var typeList = JSON.parse(callData.msg);
                $.each(typeList, function (index, value) {
                    let b = '<div class="layui-col-md2 layui-col-md-offset1"><div class="typeDiv"><button class="layui-btn typeBtn" type="button"  id = ' + value.id + ' >' + value.name + '</button>' +
                        '<button type="button" class="layui-btn editBtn" id ="edit' + value.id + '" style="margin: 0;"><i class="layui-icon layui-icon-edit" style="font-size: 30px;color: #ceeaff;"></i></button>' +
                        '<button type="button" class="layui-btn deleteBtn" id ="delete' + value.id + '" style="margin: 0;"><i class="layui-icon layui-icon-delete" style="font-size: 30px;color: #ceeaff;"></i></button></div></div>';
                    add.before(b);
                    $('#' + value.id).click(function () {
                        admin.req("type/jump/" + value.id, {}, function (callData) {
                            if (callData.code === 200) {
                                mirrorSource();
                                //window.location.href = callData.msg;
                            } else {
                                layer.msg(callData.msg, {icon: 2, time: 1000},function () {
                                    $("#refresh").click();
                                });
                            }
                        }, 'GET')
                    });
                    $('#edit' + value.id).click(function () {
                        admin.req("type/edit/" + value.id, {}, function (callData) {
                            if (callData.code === 200) {
                                let type = JSON.parse(callData.msg);
                                showEditModel(type);
                            } else {
                                layer.msg(callData.msg, {icon: 2, time: 1000},function () {
                                    $("#refresh").click();
                                });
                            }
                        }, 'GET');
                    });
                    $('#delete' + value.id).click(function () {
                        layer.confirm('确定删除该项吗？',{icon: 3, title:'提示'},function (index) {
                            admin.req("type/" +  value.id, {}, function (callData) {
                                if (callData.code === 200) {
                                    layer.msg(callData.msg, {icon: 1, time: 1000},function () {
                                        $("#refresh").click();
                                    });
                                } else {
                                    layer.msg(callData.msg, {icon: 2, time: 1000},function () {
                                        $("#refresh").click();
                                    });
                                }
                            }, 'DELETE');
                            layer.close(index);
                        })
                    })
                });
            } else {
                layer.msg(callData.msg, {icon: 2, time: 1000});
            }

        }, 'GET');


        $("#addBtn").click(function () {
            showEditModel();
        });

        function mirrorSource() {
            admin.open({
                type: 1,
                area: ['100%', '100%'],
                offset: 'auto',
                title: 'JX入口地址',
                content: $('#msSource').html(),
                success: function (layero, index) {
                    layui.use(['layer', 'form', 'table', 'util', 'formSelects', 'admin', 'config', 'laydate'], function () {
                        var $ = layui.jquery;
                        var layer = layui.layer;
                        var form = layui.form;
                        var table = layui.table;
                        var util = layui.util;
                        var formSelects = layui.formSelects;
                        var admin = layui.admin;
                        var config = layui.config;
                        var laydate = layui.laydate;
                        var mRoles = [];  // 全部角色
                        // laydate.render({
                        //     elem: '#startTime'
                        //     , type: 'datetime'
                        // });
                        // laydate.render({
                        //     elem: '#endTime'
                        //     , type: 'datetime'
                        // });
                        //初始化执行周期map
                        function initJobCycle() {
                            $.ajaxSettings.async = false;
                            var a = $.get('/v1/source/getCycle/?access_token=' + config.getToken()).done(function (data) {
                                return data.msg;
                            }).responseJSON;
                            if (a.code !== 200) return null;
                            var cycleMap = new Map();
                            var json = JSON.parse(a.msg);
                            json.forEach(function (i, v) {
                                cycleMap.set(i.id, i.comment);
                            });
                            $.ajaxSettings.async = true;
                            return cycleMap;
                        }

                        const cycleMap = initJobCycle();
                        cycleMap.forEach(function (v, k) {
                            $("#cycle").append("<option value=" + k + "> " + v + "</option>");
                        });
                        form.verify({
                            percent: function (value, item) {
                                if (value) {
                                    if (isNaN(value) || value > 100 || value < 0) {
                                        return "只能输入0到100的数字！";
                                    }
                                }
                            }
                            , datetime: function (value, item) {
                                if (value && !new RegExp(/((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\s([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/).test(value)) {
                                    return "时间格式不正确！";
                                }
                            },
                            status: function (value, item) {
                                if (value) {
                                    if (isNaN(value) || value > 2 || value < 0) {
                                        return "只能输入状态码";
                                    }
                                }
                            }
                        });

                        form.render();
                        var tableOptions = {
                            elem: '#sourceTable',
                            url: config.base_server + 'source',
                            where: {
                                access_token: config.getToken()
                            },
                            even: false, //关闭隔行背景
                            toolbar: '#toolbarDemo',
                            page: true,
                            limit: 20,
                            cellMinWidth: 100,
                            cols: [[
                                {type: 'checkbox'},
                                {field: 'id', title: 'ID', width: 70},
                                {
                                    field: 'url', title: '发布地址', width: 400, templet: function (d) {
                                        return '<a href=' + d.url + ' target="_blank">' + d.url + '</a>';
                                    }
                                },
                                {
                                    field: 'publishTime', title: '最新发布时间', width: 200, sort: true, templet: function (d) {
                                        if (d.publishTime) {
                                            return layui.util.toDateString(d.publishTime, 'yyyy-MM-dd HH:mm:ss');
                                        } else return "暂无";
                                    }
                                },
                                {
                                    field: 'jobCycle', title: '任务执行周期', templet: function (d) {
                                        // return getJobCycle(d)
                                        return cycleMap === null ? getJobCycle(d) : cycleMap.get(d.jobCycleId) === undefined ? "暂未设置" : cycleMap.get(d.jobCycleId);
                                    }
                                    , width: 200
                                },
                                {
                                    field: 'jobStatus', title: '任务执行状态', templet: function (d) {
                                        return getJobStatus(d);
                                    }
                                },
                                {
                                    field: 'assignTime', title: '下发时间', templet: function (d) {
                                        if (d.assignTime) {
                                            return layui.util.toDateString(d.assignTime, 'yyyy-MM-dd HH:mm:ss');
                                        } else return "暂无";
                                    }
                                },
                                {field: 'title', title: '标题', hide: true},
                                {field: 'depth', title: '采集深度', hide: true},
                                {
                                    field: 'saveSession', title: '保存会话', templet: function (d) {
                                        if (d.saveSession === 1) return '是';
                                        return '否';
                                    }, hide: true
                                },
                                {field: 'summary', title: '摘要', hide: true},
                                {field: 'score', title: '打分', hide: true},
                                {field: 'keywordId', title: '关键词id', hide: true},
                                {field: 'gmtCreate', title: '创建时间', hide: true},
                                {field: 'gmtModified', title: '修改时间', hide: true},
                                {align: 'center', toolbar: '#sourceTableBar', title: '操作', width: 230, fixed: 'right'}
                            ]], done: function (res) {
                                res.data.forEach(function (item, index) {
                                    var curr = $(".layui-table-body tbody tr[data-index='" + index + "']");
                                    //存在更新
                                    if (item.hasUpdate === 1) {
                                        var index;
                                        curr.css({'border': '2px solid #e67e22'});
                                        curr.hover(function (e) {
                                            index = layer.msg('网站有更新', {id: 'hoverMsg', offset: [e.pageY, e.pageX]})
                                        }, function () {
                                            layer.close(index);
                                        })
                                    }
                                    //失效网站
                                    if (item.effect === 0) {
                                        curr.css({'color': "#95a5a6"});
                                        $(".layui-table-body tbody tr[data-index='" + index + "']>td:nth-child(3) > div > a").css({'color': "#95a5a6"});
                                    }

                                });
                            }
                        };
                        // 渲染表格
                        table.render(tableOptions);
                        // 搜索按钮点击事件
                        $('#sourceBtnSearch').click(function () {
                            var url = $('#url').val().trim();
                            var cycle = $('#cycle').val().trim();
                            table.reload('sourceTable', {
                                where: {
                                    url: url,
                                    jobCycleId: cycle
                                }
                            });
                        });

                        //重置按钮点击事件
                        $('#clearBtn').click(function () {
                            $('#url').val('');
                            $('#cycle').val('');
                            table.render(tableOptions);
                            form.render();
                        });

                        $('#execAll').click(function () {
                            layer.confirm('确定全部执行一次吗', function (index) {
                                admin.req('source/execAll/', {}, function (callData) {
                                    if (callData.code === 200) {
                                        layer.msg(callData.msg, {icon: 1, time: 1000}, function () {
                                            table.render(tableOptions);
                                        });
                                    } else {
                                        layer.msg(callData.msg, {icon: 2, time: 1000}, function () {
                                            table.render(tableOptions);
                                        });
                                    }
                                }, 'GET');
                                layer.close(index);
                            });
                        });

                        $('#startAll').click(function () {
                            layer.confirm('确定开启所有任务吗', function (index) {
                                admin.req('source/startAll/', {}, function (callData) {
                                    if (callData.code === 200) {
                                        layer.msg(callData.msg, {icon: 1, time: 1000}, function () {
                                            table.render(tableOptions);
                                        });
                                    } else {
                                        layer.msg(callData.msg, {icon: 2, time: 1000}, function () {
                                            table.render(tableOptions);
                                        });
                                    }
                                }, 'GET');
                                layer.close(index);
                            });
                        });

                        $('#stopAll').click(function () {
                            layer.confirm('确定停止所有任务吗', function (index) {
                                admin.req('source/stopAll/', {}, function (callData) {
                                    if (callData.code === 200) {
                                        layer.msg(callData.msg, {icon: 1, time: 1000}, function () {
                                            table.render(tableOptions);
                                        });
                                    } else {
                                        layer.msg(callData.msg, {icon: 2, time: 1000}, function () {
                                            table.render(tableOptions);
                                        });
                                    }
                                }, 'GET');
                                layer.close(index);
                            });
                        });

                        $('#infoSync').click(function () {
                            layer.confirm('确定执行更新信息任务吗', function (index) {
                                admin.req('source/infoSync/', {}, function (callData) {
                                    if (callData.code === 200) {
                                        layer.msg(callData.msg, {icon: 1, time: 1000}, function () {
                                            table.render(tableOptions);
                                        });
                                    } else {
                                        layer.msg(callData.msg, {icon: 2, time: 1000}, function () {
                                            table.render(tableOptions);
                                        });
                                    }
                                }, 'GET');
                                layer.close(index);
                            });
                        });
                        $('#batchAdd').click(function () {
                            admin.open({
                                type: 1,
                                area: '600px',
                                offset: '65px',
                                title: '批量导入入口地址',
                                content: $('#batchAddForm').html(),
                                success: function (layero, index) {
                                    cycleMap.forEach(function (v, k) {
                                        $("#batchJobCycleId").append("<option value=" + k + "> " + v + "</option>");
                                    });
                                    // 表单提交事件
                                    form.on('submit(batchAddFormSubmit)', function (d) {
                                        layer.load(2);
                                        admin.req('source/batchAdd', d.field, function (res) {
                                            layer.closeAll('loading');
                                            if (res.code == 200) {
                                                layer.msg(res.msg, {icon: 1, time: 1000});
                                                layer.closeAll('page');
                                                table.reload('sourceTable');
                                            } else {
                                                layer.msg(res.msg, {icon: 2, time: 1000});
                                            }
                                        }, 'POST');
                                        return false;
                                    });
                                    form.render();
                                }

                            });
                        });

                        // 工具条点击事件
                        table.on('tool(sourceTable)', function (obj) {
                            var data = obj.data;
                            var layEvent = obj.event;

                            /*if (layEvent === 'del') { // 删除
                                layer.confirm('确定删除此数据吗？', {
                                    skin: 'layui-layer-admin'
                                }, function (i) {
                                    layer.close(i);
                                    layer.load(2);
                                    admin.req('appManage/' + obj.data.id, {}, function (data) {
                                        layer.closeAll('loading');
                                        if (data.code == 200) {
                                            layer.msg(data.msg, {icon: 1});
                                            obj.del();
                                        } else {
                                            layer.msg(data.msg, {icon: 2});
                                        }
                                    }, 'DELETE');
                                });
                            } else */
                            if (layEvent === 'edit') { // 修改数据
                                showEditModel(data);
                            } else if (layEvent === 'exec') {//执行一次任务
                                layer.confirm('确定执行吗', function (index) {
                                    admin.req('source/exec/' + data.id, {}, function (callData) {
                                        if (callData.code === 200) {
                                            layer.msg(callData.msg, {icon: 1, time: 1000}, function () {
                                                table.render(tableOptions);
                                            });
                                        } else {
                                            layer.msg(callData.msg, {icon: 2, time: 1000}, function () {
                                                table.render(tableOptions);
                                            });
                                        }
                                    }, 'GET');
                                    layer.close(index);
                                });
                            } else if (layEvent === 'start') { //开始任务
                                layer.confirm('确定开始吗', function (index) {
                                    admin.req('source/start/' + data.id, {}, function (callData) {
                                        if (callData.code === 200) {
                                            layer.msg(callData.msg, {icon: 1, time: 1000}, function () {
                                                table.render(tableOptions);
                                            });
                                        } else {
                                            layer.msg(callData.msg, {icon: 2, time: 1000}, function () {
                                                table.render(tableOptions);
                                            });
                                        }
                                    }, 'GET');
                                    layer.close(index);
                                });
                            } else if (layEvent === 'stop') { //停止任务
                                layer.confirm('确定停止吗', function (index) {
                                    admin.req('source/stop/' + data.id, {}, function (callData) {
                                        if (callData.code === 200) {
                                            layer.msg(callData.msg, {icon: 1, time: 1000}, function () {
                                                table.render(tableOptions);
                                            });
                                        } else {
                                            layer.msg(callData.msg, {icon: 2, time: 1000}, function () {
                                                table.render(tableOptions);
                                            });
                                        }
                                    }, 'GET');
                                    layer.close(index);
                                });
                            }
                        });


                        //头工具栏事件
                        table.on('toolbar(sourceTable)', function (obj) {
                            var checkStatus = table.checkStatus(obj.config.id);
                            switch (obj.event) {
                                case 'toolBarAdd':
                                    showEditModel();
                                    break;
                                case 'batchDel':
                                    var data = checkStatus.data;
                                    batchDel(data);
                                    break;
                            }
                        });

                        // 显示表单弹窗
                        function showEditModel(data) {
                            admin.open({
                                type: 1,
                                area: '980px',
                                offset: '65px',
                                title: data ? '修改入口地址信息' : '添加入口地址信息',
                                content: $('#sourceForm').html(),
                                success: function (layero, index) {
                                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                                    laydate.render({
                                        elem: '#assignTime'
                                        , type: 'datetime'
                                    });
                                    cycleMap.forEach(function (v, k) {
                                        $("#jobCycleId").append("<option value=" + k + "> " + v + "</option>");
                                    });
                                    var tip_index = 0;
                                    var t = $('.layui-form-label');
                                    for (let i = 0; i < t.length; i++) {
                                        let item = $(t[i]);
                                        if (item.attr("desc") && item.attr("desc") !== '') {
                                            item.on('mouseenter', item, function () {
                                                tip_index = layer.tips(item.attr("desc"), item, {
                                                    anim: 5,
                                                    tips: [3, '#CFD8DC'],
                                                    time: 0
                                                });
                                            }).on('mouseleave', item, function () {
                                                layer.close(tip_index);
                                            });
                                        }
                                    }
                                    // 回显数据
                                    if (data) {
                                        form.val('sourceForm', data);
                                        $("form input").on('change', function () {
                                            $(this).addClass("modified");
                                        });
                                        var u = $(".unmodify");
                                        u.addClass('final');
                                        u.attr('disabled', 'disabled');
                                    }
                                    // 表单提交事件
                                    form.on('submit(sourceFormSubmit)', function (d) {
                                        layer.load(2);
                                        admin.req('source', d.field, function (res) {
                                            layer.closeAll('loading');
                                            if (res.code == 200) {
                                                layer.msg(res.msg, {icon: 1, time: 1000});
                                                layer.closeAll('page');
                                                table.reload('sourceTable');
                                            } else {
                                                layer.msg(res.msg, {icon: 2, time: 1000});
                                            }
                                        }, data ? 'PUT' : 'POST');
                                        return false;
                                    });
                                    form.render();
                                }
                            });
                        }

                        function batchDel(data) {
                            if (data.length < 1) {
                                layer.msg("没有选择任何一行!")
                            } else {
                                layer.confirm('确定要删除选中的行吗', function (index) {
                                    var ids = [];
                                    data.forEach(function (val) {
                                        ids.push(val.id);
                                    });
                                    admin.req('source/batchDel/' + ids, {}, function (callData) {
                                        if (callData.code === 200) {
                                            layer.msg(callData.msg, {icon: 1, time: 1000});
                                        } else {
                                            layer.msg(callData.msg, {icon: 2, time: 1000});
                                        }
                                    }, 'DELETE');
                                    table.render(tableOptions);
                                    layer.close(index);
                                });
                            }
                        }

                        // 获取所有角色
                        layer.load(2);
                        admin.req('role', {}, function (res) {
                            layer.closeAll('loading');
                            if (0 == res.code) {
                                mRoles = res.data;
                            } else {
                                layer.msg('获取角色失败', {icon: 2});
                            }
                        }, 'GET');

                        function getJobStatus(d) {
                            switch (d.jobStatus) {
                                case 0: {
                                    return "未执行";
                                }
                                case 1: {
                                    return "正在执行"
                                }
                                case 2: {
                                    return "已停止"
                                }
                                default : {
                                    return d.jobStatus ? d.jobStatus : "";
                                }
                            }
                        }

                        function getJobCycle(d) {
                            if (d.jobCycleId === undefined) return "暂无";
                            $.ajaxSettings.async = false;
                            var a = $.get('/v1/source/getCycle/' + d.jobCycleId + '?access_token=' + config.getToken()).done(function (data) {
                                return data.msg;
                            }).responseJSON.msg;
                            $.ajaxSettings.async = true;
                            return a;
                        }
                    })
                }
            });
        }
        
        // 显示表单弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: ['350px', '250px'],
                offset: 'auto',
                title: data ? '修改网页类型' : '添加网页类型',
                content: $('#typeForm').html(),
                success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    // 回显数据
                    if (data) {
                        form.val('typeForm', data);
                    }
                    // 表单提交事件
                    form.on('submit(typeFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req('type', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    $("#refresh").click();
                                });
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2, time: 1000});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }


    });

</script>